package com.example.mtblog.controller;

import com.example.mtblog.common.Result;
import com.example.mtblog.common.common;
import com.example.mtblog.dto.*;
import com.example.mtblog.dto.UserPrivacy;
import com.example.mtblog.entity.User;
import com.example.mtblog.service.ArticleService;
import com.example.mtblog.service.UserService;
import com.example.mtblog.tool.StrUtils;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/personal")
public class PersonalController {

    @Resource
    UserService userService;


    @Resource
    ArticleService articleService;

    /**
    * @description: demo
    * @author MT
    * @date 2023/10/25 14:49
     */
    @GetMapping("/demo")
    public Result<?> demo(@RequestParam(value = "uid",defaultValue = "0") Integer uid){
        try {
            System.out.println("dmeo");
        }catch (Exception e){
            String msg = "error in personal/get " + "\n"  +e ;
            log.error(msg);
            return Result.error(msg);
        }
        return  Result.success();
    }

    /**
    * @description: 加入信息，比如是否是朋友关系
    * @author MT
    * @date 2023/10/30 21:08
    */


     public UserDTO initUserDto(User user,Integer uid){



         UserDTO userDTO = new UserDTO(user);

         userDTO.setPassword(null);
         if(uid!=-1) {
             UserInfo userInfo = common.getUserInfo(uid);
             UserPrivacy userPrivacy = common.getUserPrivacy(uid);
             Boolean isFriend = StrUtils.judge(String.valueOf(user.getUid()), userInfo.getFriends());
             userDTO.setIsFriend(isFriend);
             userDTO.setUserPrivacy(userPrivacy);
             userDTO.setUserInfo(userInfo.export(userInfo));
         }else userDTO.setIsFriend(false);
        return userDTO;
     }



    /**
     * @description: 得到个人主页信息
     * @author MT
     * @date 2023/10/25 14:49
     */
    @GetMapping("/get")
    public Result<?> getPersonalInfo(@RequestParam(value = "author",defaultValue = "0") Integer author,
                                     @RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo,
                                     @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize,
                                     HttpServletRequest request){
        Map<String,Object> map = new HashMap<>();
        try {
            if(author == 0)
                return  Result.error("请求参数错误");

            Integer uid = common.getLoginUid(request);
            User user = userService.selectBy("uid", String.valueOf(author));


            if(user == null){
                return Result.error(204,"用户未找到");
            }


            return  Result.success(initUserDto(user,uid));



        }catch (Exception e){
            String msg = "error in personal/get " + "\n" ;
            log.error(msg + e);
            return Result.error(msg);
        }

    }
}


